package com.wyzz.hr.action;

import java.io.File;
import java.io.FileInputStream;
import java.util.*;
import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.base.Status;
import com.wyzz.hr.base.io.Exportinfo;
import com.wyzz.hr.bo.ICompaplanBo;
import com.wyzz.hr.bo.IEmpSalaryAcctitemsBo;
import com.wyzz.hr.exportfile.ExportSalaryAdj;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalaryadj;
import com.wyzz.hr.pojo.Emptype;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.pojo.Statusconf;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.util.impl.PropertiesFileConfigManager;

public class SearchCompaplan extends BaseAction implements Status {

	public SearchCompaplan() {
		searchOrExport = null;
		docStream = null;
		contentDisposition = null;
		compaplan = new Empsalaryadj();
		page = new Pager();
		jobgradePro = new ArrayList();
		emp = new Employee();
		emp.setEmpStatus(Integer.valueOf(1));
	}

	public String execute() throws Exception {
		DetachedCriteria dc = searchPayAdjHist_DC();
		addCriteria(dc);
		getDrillDownList();
		ICompaplanBo compaplanBo = (ICompaplanBo) getBean("compaplanBo");
		if ("export".equals(this.searchOrExport)) {
			try {
				List exportResult = compaplanBo.mysearchCompaplan(dc, null);
				if ((exportResult == null) || (exportResult.size() == 0)) {
					addActionError("无调薪计划数据可以导出！");
					return "success";
				}
				Exportinfo exportSalary = new Exportinfo();
				IEmpSalaryAcctitemsBo esaitemsBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
						.getBean("empsalaryacctitemsBo");
				List[] titles = exportSalary.getSalaryadjSheetAndTitle(
						exportResult, esaitemsBo);
				List[] results = exportSalary.getSalaryadjResult(exportResult,
						titles);
				String filePath = FileOperate.getFileHomePath();
				String xslFileName = "export-"
						+ FileOperate.buildFileName()
						+ PropertiesFileConfigManager.getInstance()
								.getProperty("sys.saveDir.type").trim();
				filePath = filePath + xslFileName;
				ExportSalaryAdj export = new ExportSalaryAdj(results, titles);
				export.export(filePath);
				File file = new File(filePath);
				FileInputStream fileInputStream = new FileInputStream(file);
				setDocStream(fileInputStream);
				String downFileName = "salaryAdj.xls";
				setContentDisposition("filename=\"" + downFileName + "\"");
				setFileName(xslFileName);
				clearErrorsAndMessages();
				return "download";
			} catch (Exception e) {
				addActionError("数据导出失败！");
				e.printStackTrace();
				return "success";
			}
		}
		this.compaplanList = compaplanBo.mysearchCompaplan(dc, this.page);
		if (this.compaplanList.isEmpty())
			return "success";
		compaplanBo.calcSalaryAdj(this.compaplanList);
		return "success";
	}

	private DetachedCriteria searchPayAdjHist_DC() {
		DetachedCriteria dc = DetachedCriteria.forClass(Empsalaryadj.class);
		dc.setFetchMode("esaEmpno.empDeptNo", FetchMode.JOIN);
		dc.setFetchMode("esaEmpno.empType", FetchMode.JOIN);
		dc.setFetchMode("esaEmpno.empLocationNo", FetchMode.JOIN);
		dc.setFetchMode("esaEcptypeId", FetchMode.JOIN);
		dc.setFetchMode("esaJobgradeCur", FetchMode.JOIN);
		dc.setFetchMode("esaJobgradePro", FetchMode.JOIN);
		dc.setFetchMode("esaEsavIdCur", FetchMode.JOIN);
		dc.setFetchMode("esaEsavIdPro", FetchMode.JOIN);
		dc.createAlias("esaEmpno", "emp");
		return dc;
	}

	private void addCriteria(DetachedCriteria dc) {
		BaseCrit.addEmpDC(dc, "emp", emp.getEmpName());
		BaseCrit.addDeptDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), (new StringBuilder())
				.append("emp.").append(Employee.PROP_EMP_PB_NO).toString(),
				null, emp.getEmpDeptNo());
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_TYPE).toString(), Emptype.PROP_ID,
				new Object[] { emp.getEmpType() });
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_LOCATION_NO).toString(), Location.PROP_ID,
				new Object[] { emp.getEmpLocationNo() });
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_STATUS).toString(), "eq", new Integer[] { emp
				.getEmpStatus() });
		BaseCrit.addDC(dc, Empsalaryadj.PROP_ESA_STATUS, "eq",
				new Integer[] { compaplan.getEsaStatus() });
	}

	private void getDrillDownList() {
		locations = getDrillDown("Location", new String[0]);
		empType = getDrillDown("EmpType", new String[0]);
		jobgradePro = getDrillDown("Jobgrade", new String[0]);
		acctVersionList = getDrillDown("EmpSalaryAcct", new String[0]);
		status = getDrillDown("CompaplanStatus", new String[0]);
		empStatus = getDrillDown("EmpStatus", new String[0]);
	}

	/**
	 * @deprecated Method getEcpStatus is deprecated
	 */

	public String getEcpStatus(int statusNo) {
		for (Iterator i$ = status.iterator(); i$.hasNext();) {
			Statusconf stconf = (Statusconf) i$.next();
			if (stconf.getId().getStatusconfNo().intValue() == statusNo)
				return stconf.getStatusconfDesc();
		}

		return "";
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public Empsalaryadj getCompaplan() {
		return compaplan;
	}

	public void setCompaplan(Empsalaryadj compaplan) {
		this.compaplan = compaplan;
	}

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public List getCompaplanList() {
		return compaplanList;
	}

	public void setCompaplanList(List compaplanList) {
		this.compaplanList = compaplanList;
	}

	public List getEmpStatus() {
		return empStatus;
	}

	public void setEmpStatus(List empStatus) {
		this.empStatus = empStatus;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public String getContentDisposition() {
		return contentDisposition;
	}

	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

	public FileInputStream getDocStream() {
		return docStream;
	}

	public void setDocStream(FileInputStream docStream) {
		this.docStream = docStream;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public List getJobgradePro() {
		return jobgradePro;
	}

	public void setJobgradePro(List jobgradePro) {
		this.jobgradePro = jobgradePro;
	}

	public List getAcctVersionList() {
		return acctVersionList;
	}

	public void setAcctVersionList(List acctVersionList) {
		this.acctVersionList = acctVersionList;
	}

	public List getEmpType() {
		return empType;
	}

	public void setEmpType(List empType) {
		this.empType = empType;
	}

	public List getLocations() {
		return locations;
	}

	public void setLocations(List locations) {
		this.locations = locations;
	}

	public List getStatus() {
		return status;
	}

	public void setStatus(List status) {
		this.status = status;
	}

	public String getSyscoments() {
		return syscoments;
	}

	public void setSyscoments(String syscoments) {
		this.syscoments = syscoments;
	}

	private static final long serialVersionUID = 0xfffffff49812ce47L;
	private String searchOrExport;
	private FileInputStream docStream;
	private String contentDisposition;
	private String fileName;
	private Empsalaryadj compaplan;
	private Employee emp;
	private Pager page;
	private String syscoments;
	private List jobgradePro;
	private List acctVersionList;
	private List compaplanList;
	private List locations;
	private List empType;
	private List empStatus;
	private List status;
}
